<template>
  <div class="user-container">
    <!-- 用户个人资料 -->
    <div class="user-profile">
      <div class="info">
        <van-image round :src="userInfo.photo" />
        <h3 class="name">
          {{ userInfo.name }}
          <br />
          <van-tag size="mini">申请认证</van-tag>
        </h3>
      </div>
      <van-row>
        <van-col span="8">
          <p>0</p>
          <p>动态</p>
        </van-col>
        <van-col span="8">
          <p>{{ userInfo.follow_count }}</p>
          <p>关注</p>
        </van-col>
        <van-col span="8">
          <p>{{ userInfo.fans_count }}</p>
          <p>粉丝</p>
        </van-col>
      </van-row>
    </div>

    <!--
      编辑入口
      to属性用来设置跳转路由地址
      is-link属性用来指定可点击行为
     -->
    <van-cell-group class="user-group">
      <van-cell icon="edit" title="编辑资料" to="/editUser" is-link />
      <van-cell icon="chat-o" title="小智同学" to="/chat" is-link />
      <van-cell icon="warning-o" title="退出登录" @click="clickQuit" is-link />
    </van-cell-group>
    <!--  -->
  </div>
</template>

<script>
import { getUserInfo } from '@/api/user'
export default {
  name: 'UserIndex',
  data () {
    return {
      userInfo: {} // 存储用户信息
    }
  },
  methods: {
    // 获取用户信息
    hGetUserInfo () {
      getUserInfo().then(res => {
        // console.log(res)
        this.userInfo = res.data.data
        // 把用户数据在vuex里也保存一份
        this.$store.commit('setUserInfo', res.data.data)
      })
    },
    // 点击退出
    clickQuit () {
      this.$dialog.confirm({
        title: '系统提示',
        message: '确定要退出吗？'
      }).then(() => {
        // 调用 vuex 里mutations 的方法
        this.$store.commit('removeToken')
        // 退会登录页
        this.$router.push('/login')
      }).catch(() => {
      })
    }
  },
  mounted () {
    this.hGetUserInfo()
  }
}
</script>

<style lang="less" scoped>
.user-container{
  position: fixed;
  top:0;
  bottom: 0;
  width: 100%;
}
.user {
  &-profile {
    width: 100%;
    height: 200px;
    display: block;
    background: #3296fa;
    color: #fff;
    .info {
      display: flex;
      padding: 20px;
      align-items: center;
      .van-image {
        width: 64px;
        height: 64px;
      }
      .name {
        font-size: 16px;
        font-weight: normal;
        margin-left: 10px;
      }
      .van-tag {
        background: #fff;
        color: #3296fa;
      }
    }
    p {
      margin: 0;
      text-align: center;
    }
  }
  &-group {
    margin-bottom: 15px;
  }
  &-links {
    padding: 15px 0;
    font-size: 12px;
    text-align: center;
    background-color: #fff;
    .van-icon {
      display: block;
      font-size: 24px;
      padding-bottom: 5px;
    }
  }
}
</style>
